package com.ly.controller;


import com.ly.base.ResponseData;
import com.ly.entity.Car;
import com.ly.entity.CarOrder;
import com.ly.entity.SysUser;
import com.ly.param.CarOrderAddParam;
import com.ly.param.CarOrderParam;
import com.ly.param.CarParam;
import com.ly.service.CarOrderService;
import com.ly.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@Api(tags = "租车订单管理")
@RestController
@RequestMapping("/order")
public class CarOrderController {

    @Autowired
    private CarOrderService carOrderService;
    @Autowired
    private SysUserService sysUserService;


    @ApiOperation(value = "租车订单新增")
    @PostMapping
    public ResponseData addOrder(@RequestBody CarOrderAddParam carOrderAddParam, Principal principal){
        return carOrderService.addCarOrder(carOrderAddParam,principal);
    }


    @ApiOperation(value="修改订单")
    @PutMapping
    public ResponseData updateCarInfo(@RequestBody CarOrder carOrder, Principal principal) {

        return carOrderService.updateCarOrder(carOrder,principal);

    }


    @ApiOperation(value="分页查询订单信息")
    @GetMapping("/page")
    public ResponseData pageCarInfoList(CarOrderParam carOrderParam, Principal principal) {

        SysUser user = sysUserService.getByUserName(principal.getName());
        if(!user.getSysRole().getCode().equalsIgnoreCase("admin")){
            carOrderParam.setUserId(user.getId());
        }

        return ResponseData.success("查询成功！",carOrderService.pageCarOrderList(carOrderParam));
    }


    @ApiOperation(value="删除订单")
    @DeleteMapping("/{id}")
    public ResponseData deleteCarInfo(@PathVariable String id) {
        return carOrderService.deleteCarOrder(id);

    }


    @ApiOperation(value="查询订单详情")
    @GetMapping("/{id}")
    public ResponseData findByCarId(@PathVariable String id) {
        return ResponseData.success("查询成功！",carOrderService.findByCarOrderId(id));
    }


    @ApiOperation(value="归还租车")
    @GetMapping("/pay/{id}")
    public ResponseData orderPay(@PathVariable String id, Principal principal){
        return carOrderService.returnCar(id,principal);
    }


}
